<template>
	<view class="content">
		<view v-if="isLoading == false" class="loading-view">图片正在加载中...</view>
			<view >
				<image  class="share-img" :src="sharePosterImg" mode="widthFix" @load="loadImg()" :show-menu-by-longpress="true"></image>
			</view>
			<view class="text-tip">
				长按上面图片，保存你的活动海报
			</view>
			<!-- #ifdef H5 -->
				<view @click="share" class="share-btn">点击分享链接</view>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
				<button open-type="share" class="share-btn">点击分享链接</button>
			<!-- #endif -->
			
			<!-- 分享提示 start-->
			<uni-popup ref="shareTip" :custom="true">
				<view class="popup_box_de_share">
					<image class="popup_box_close_share" :src="imglink + 'share_tip.png'"></image>
				</view>
			</uni-popup>
			<!-- 分享提示 end -->
	</view>
</template>

<script>
	import uniPopup from '@/components/uni-popup/uni-popup.vue';
	
	//#ifdef H5
	import wx from '@/common/wx-js-sdk.js'
	//#endif
	// import QRCode from 'qrcode'
	export default {
		components: {
			uniPopup
		},
		// #ifdef MP-WEIXIN
			onShareAppMessage(res) {
				this.setActivityShare()
				return {
					// title: this.itemAct.name,
					title: this.customer_info.nickname + '邀请您加入' + this.itemAct.name + '活动', // 分享描述
					path: '/pages/home/organization/activeDetail-3?store_id=' + this.$store.getters.getStoreId +
						'&actId=' + this.itemAct.activity_id + '&share_id=' + this.$store.getters.getCustomerId +
						'&page=share' + '&invite_type=activity', // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					imageUrl: this.itemAct.image[0].image,
				}
			},
		// #endif
		onLoad(options) {
			this.activity_id = options.actId
			
			let store_id = parseInt(options.store_id,10);
			if(!store_id>0){
				store_id = uni.getStorageSync('store_id')
			}else{
				uni.setStorageSync('store_id', store_id)
			}
			this.store_id = store_id
			this.$store.commit('updateStoreId',store_id)
			
			this.goodInfo = uni.getStorageSync('shareAct')
			console.log(this.goodInfo)
			uni.showLoading({title: '图片正在加载中...',mask:true});
			this.getCustomer()
			// this.drawCanvas()
		},
		data() {
			return {
				imglink: this.$store.state.imgLink,
				info:{},
				goodInfo:'',
				token: this.$store.getters.getToken,
				// userId: this.$store.getters.getUserId,
				select: false,
				store_id:'',
				activity_id:'',
				itemAct:"",
				customer_info:{},
				sharePosterImg:'',
				isLoading:false
			};
		},
		// mounted() {
			
		// },
		methods: {
			// 添加活动分享次数
			setActivityShare(){
				let data = {
					token: this.$store.getters.getToken,
					activity_id: this.itemAct.activity_id
				}
				this.$api.add_activity_share(data).then(res => {
					console.log(res)
					
				}).catch(err => {
					console.log(err)
				})
			},
			//#ifdef H5
			async sharePatriarch(itemAct,customer_info){
				console.log(itemAct,'AAAAAAAAAAAAAAA');
				let signLink = /(Android)/i.test(navigator.userAgent) ? location.href : window.entryUrl;
				console.log(signLink,'signLink')
				let data = {
					token:this.$store.getters.getToken,
					url:signLink
				}
				// console.log(this.store_info,'1111111')
				let _self = this
				try{
					
					let configData = JSON.parse((await this.$api.gain_share_data(data)).data)
					
					wx.config(configData)
					wx.ready(function(){
						wx.checkJsApi({
							jsApiList: [
								'updateTimelineShareData',
								'updateAppMessageShareData',
								'onMenuShareAppMessage',
								'onMenuShareTimeline'
							],
							success: function(res) {
								console.log('接口检测成功:'+res);
							},
							error: function(err) {
								console.log('接口检测失败:'+err);
							}
						})
							
						const shareData = { 
							title: itemAct.name, // 分享标题
							desc: customer_info.nickname + '邀请您加入' + itemAct.name + '活动', // 分享描述
							link: _self.current_url()+'/pages/home/organization/activeDetail-3?store_id=' + _self.store_id + '&actId=' + itemAct.activity_id + '&customer_id=' + customer_info.customer_id + '&page=share',
							imgUrl: itemAct.image[0].image, // 分享图标
							success: function (res) {
							  console.log(res,'success ---')
							},
							fail:function(err){
								console.log(err.errMsg,'fail ---')
							}
						}
						wx.updateAppMessageShareData(shareData);
						wx.updateTimelineShareData(shareData);
					})
					
				}catch(e){
					console.log(e)
				}
			},
			//#endif
			
			// 获取所有活动信息
			getActList(){
				uni.showToast({
				    title: '标题',
				    duration: 2000
				});
				this.page = 1
				let data = {
					token:this.$store.getters.getToken,
					activity_id:this.activity_id
				}
				this.$api.gain_all_act(data).then(res => {
					console.log(res)
					if(res.status){
						this.itemAct = res.list.data[0]
						console.log(this.itemAct,'0000000000000000');
						// #ifdef H5
							this.sharePatriarch(this.itemAct,this.customer_info)
						// #endif
					}else{
						uni.showToast({
							title:res.msg,
							icon:"none",
							duration:2000
						})
					}
					this.setList()
					setTimeout(()=>{
						this.beginCanvas()
					},500)
				}).catch(err => {
					console.log(err)
				})
			},
			getCustomer(){
				
				const data = {
					token:this.$store.getters.getToken,
					store_id:this.store_id
				}
				this.$api.getCustomerInfo(data).then(res => {
					console.log(res)
					if(res.status){
						this.customer_info = res.data.userinfo
						console.log(this.customer_info,'CCCCCCCCCCCCCCCCCCC');
						this.getActList()
						this.getActSharePosters()
					}
				}).catch(err => {
					console.log(err)
				})
				// let customer_info = this.$api.getCustomerInfo(data)
				// console.log(this.customer_info)
				
			},
			// 获取海报
			getActSharePosters(){
				const data = {
					token:this.$store.getters.getToken,
					activity_id : this.activity_id,
					customer_id : this.$store.getters.getCustomerId
				}
				console.log(data,"获取海报");
				this.$api.getActSharePosters(data).then(res => {
					if(res.status){
						this.sharePosterImg = res.data
					}else{
						uni.showToast({
							icon:'none',
							title:res.msg,
						})
					}
				})
			},
			share(){
				this.$refs.shareTip.open()
				this.sharePatriarch(this.itemAct,this.customer_info)
			},
			loadImg(){
				setTimeout(function () {uni.hideLoading();}, 100);
				console.log("图片加载完成");
				this.isLoading = true
			}

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
.share-img{
	margin-top: 30rpx;
	border-radius: 5rpx;
	box-shadow: 0px 4rpx 12rpx 0px rgba(0, 0, 0, 0.4);
}
.text-tip{
	margin-top: 20rpx;
}
/* 分享提示 start */
	.popup_box_de_share {
		width: 100%;
		height: 100%;
		z-index: 999;
	}

	.popup_box_close_share {
		width: 300upx;
		height: 300upx;
		position: fixed;
		top: 10upx;
		right: 10upx;
	}

	/* 分享提示 end */
	.share-btn{
		height: 80rpx;
		width: 638rpx;
		color: #FFFFFF;
		font-size: 32rpx;
		line-height: 80rpx;
		text-align: center;
		margin: 40rpx auto;
		background-image: linear-gradient(90deg, #FE9543 14%, #FF664F 100%);
		border-radius: 44rpx;
	}
	.loading-view{
		position: absolute;
		top: 40rpx;
		font-size: 40rpx;
		color: #999999;
		height: 500rpx;
		width: 690rpx;
		text-align: center;
		line-height: 758rpx;
	}
</style>
